package top.maof.book.spider.develop.contents.yanmoxuan;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.Date;

/**
 * 衍墨轩
 * <p>
 * 无法抓取
 */@Slf4j
@Data
@TargetUrl({"http://www.yanmoxuan.net/text_\\d+.html"})
@HelpUrl({"http://www.yanmoxuan.net/xuanhuan/*", "http://www.yanmoxuan.net/qihuan/*", "http://www.yanmoxuan.net/xiuzhen/*",
        "http://www.yanmoxuan.net/dushi/*", "http://www.yanmoxuan.net/yanqing/*", "http://www.yanmoxuan.net/lishi/*",
        "http://www.yanmoxuan.net/tongren/*", "http://www.yanmoxuan.net/wuxia/*", "http://www.yanmoxuan.net/kehuan/*",
        "http://www.yanmoxuan.net/youxi/*", "http://www.yanmoxuan.net/junshi/*", "http://www.yanmoxuan.net/jingji/*",
        "http://www.yanmoxuan.net/lingyi/*", "http://www.yanmoxuan.net/qita/*", "http://www.yanmoxuan.net/text_\\d+.html"})
public class Ymxxs implements AfterExtractor, Split {
    @ExtractBy(value = "//article[@class=info]/header[@class=line]/h1/text()")
    private String name;

    @ExtractBy(value = "//article[@class=info]/p[@class=pt20]/i[1]/a/text()")
    private String author;

    @ExtractBy(value = "//article[@class=info]/div[@class=cover]/img/@src")
    private String img;

    @ExtractBy(value = "//article[@class=info]/p[@class=pt20]/i[2]/a/text()")
    private String classify;

    //@ExtractBy(value = "//div[@id=maininfo]/div[@class=info]/p[@class=s]/text()")//更新时间：2019-07-10 08:51
    private Date updateDate;

    private Date insertDate = new Date();

    @ExtractBy(value = "//article[@class=info]/p[@class=desc]/text()")
    private String synopsis;

    private String state;

    private String url;

    private String content;

    private Integer webId = 6;

    public static final String CHARSET = "utf-8";

    @Override
    public void afterProcess(Page page) {
        // 章节目录和小说介绍不再同一页面,所以章节目录url要通过该页面解析获取
        this.url = page.getHtml().xpath("//article[@class=info]/div[@class=operate]/a[3]/@href").toString();
        if (this.url != null && this.url.startsWith("//")) {
            this.url = "https:" + this.url;
        }

        this.author = this.author.trim();

        this.name = this.name.trim();
        log.info(this.name + "-" + this.author);
    }


    @Override
    public Book getBook() {
        Book book = new Book();
        book.setName(this.name);
        book.setAuthor(this.author);
//        book.setState(this.state);
//        book.setImg(this.img);
//
//        book.setInsertDate(this.insertDate);
//        book.setClassify(this.classify);
//        book.setSynopsis(this.synopsis);
//        book.setUpdateDate(this.updateDate);
        return book;
    }

    @Override
    public Contents getContents() {
        Contents contents = new Contents();
        contents.setUrl(this.url);
        contents.setContent(this.content);
        contents.setWebId(this.webId);
        contents.setUpdateDate(this.updateDate);
        return contents;
    }
}


